using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MonoTorrent.Common;

namespace MonoTorrent.Client {
    internal class TorrentFileStream: Stream {
        #region Fields
        private Stream      stream;
        private TorrentFile file;
        private string      path;
        #endregion

        #region Properties
        #region Inherited Properties
        public override bool CanRead {
            get { return stream.CanRead; }
        }

        public override bool CanSeek {
            get { return stream.CanSeek; }
        }

        public override bool CanWrite {
            get { return stream.CanWrite; }
        }

        public override long Length {
            get { return stream.Length; }
        }

        public override long Position {
            get { return stream.Position;  }
            set {
                Console.WriteLine("TorrentFileStream.Position = {0}", value);
                stream.Position = value;
            }
        }
        #endregion

        public TorrentFile File {
            get { return file; }
        }

        public string Path {
            get { return path; }
        }
        #endregion

        #region Constructors
        public TorrentFileStream(string filePath, TorrentFile file, FileMode mode, FileAccess access, FileShare share) :
            base()
        {
            this.stream = new FileStream(filePath, mode, access, share);
            this.file   = file;
            this.path   = filePath;
        }
        #endregion

        #region Methods
        #region Inherited Methods
        public override void Flush() {
            Console.WriteLine("TorrentFileStream.Flush()");
            stream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count) {
            Console.WriteLine("TorrentFileStream.Read(byte[] buffer, int offset={0}, int count={1})", offset, count);
            return stream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin) {
            Console.WriteLine("TorrentFileStream.Seek(int offset={0}, SeekOrigin origin={1})", offset, origin);
            return stream.Seek(offset, origin);
        }

        public override void SetLength(long value) {
            Console.WriteLine("TorrentFileStream.SetLength(long value={0})", value);
            stream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count) {
            Console.WriteLine("TorrentFileStream.Write(byte[] buffer, int offset={0}, int count={1})", offset, count);
            stream.Write(buffer, offset, count);
        }
        #endregion
        #endregion
    }
}
